<template>
    <div class="internalTransactionRecordsPage">
        <div class="search-wrap d-flex a-center flex-wrap">
            <div class="search-item d-flex a-center m-l20 m-b10">
                <label>单位名称</label>
                <el-input class="m-l10" v-model="form.companyName" placeholder="请输入单位名称"></el-input>
            </div>
            <div class="search-item d-flex a-center m-l20 m-b10">
                <label>交易类型</label>
                <el-select v-model="form.tradeType" class="m-l10" >
                    <el-option label="上划" value="上划"></el-option>
                    <el-option label="下拨" value="下拨"></el-option>
                </el-select>
            </div>
            <div class="search-item d-flex a-center m-l20 m-b10">
                <label>对方账户名称</label>
                <el-input class="m-l10" v-model="form.otherAccountName" placeholder="请输入对方账户名称"></el-input>
            </div>
            <div class="search-item d-flex a-center m-l20 m-b10">
                <label>交易日期</label>
                <el-date-picker
                        class="m-l10"
                        v-model="form.dateTime"
                        value-format="YYYY-MM-DD"
                        type="daterange"
                        size="large"
                        range-separator="-"
                        start-placeholder="开始时间"
                        end-placeholder="结束时间"
                        @change="datePickerChange"
                />
            </div>
            <div class="search-buttons m-l50 d-flex a-center m-b10">
                <el-button
                        color="#fc702b"
                        style="color:#fff;"
                        @click.prevent="onQuery"
                >查询</el-button>
                <el-button
                        color="#f0f0f0"
                        style="color: #000000"
                        @click.prevent="onReset"
                >重置</el-button>
                <el-divider direction="vertical" />
                <el-button
                        class="button_edit m-r10"
                        @click="onExport()"
                >
                    <svg-icon icon-class="project-detail-icon-derive" class="icon-export m-r5"></svg-icon>
                    一键导出
                </el-button>
                <el-button
                        class="button_edit"
                        @click="onImport()"
                >
                    <svg-icon icon-class="project-detail-icon-l-xiazai" class="icon-export m-r5"></svg-icon>
                    导入
                </el-button>
            </div>
        </div>
        <public-table
                ref="publicTableRef"
                class="m-t20"
                index
                radio
                :height="550"
                :indexWidth="'100'"
                :page="page"
                :tableData="tableData"
                :tableOption.sync="tableOption"
                @page-change="onPageChange"
                @handleSelectionChange="handleSelectionChange"
        >
        </public-table>
    </div>

    <import-file-popup
            :title="title"
            :is-tip="true"
            ref="importFilePopupRef"
            :interface-config="importFileInterfaceConfig"
            :template-config="templateConfig"
            @on-success="onQuery"
    />
</template>

<script setup>
import {onMounted, reactive, ref} from "vue";
import {
    ElSelect,
    ElOption,
    ElDivider,
    ElMessage,
    ElDatePicker, ElRadioGroup, ElRadioButton
} from "element-plus";
import PublicTable from "@/components/PublicTable.vue";

import {tableCommonFun} from "@/hooks/tableCommon";
import {bankManagementApi, companyApi} from "@/api/businessFinance";
import {useRouter} from "vue-router";
import ImportFilePopup from "@/components/importFilePopup.vue";

const title = ref('导入内部户交易记录表');
const interfaceConfig = reactive({
    fileName:'businessFinance',
    apiPath:'CBS.getInnerTradeRecordList',
});
const importFileInterfaceConfig = reactive({
    fileName:'businessFinance',
    apiPath:'CBS.importInnerTradeRecord',
});
const exportConfig = reactive({
    fileName:'businessFinance',
    apiPath:'CBS.getBankInnerTradeRecordLeadingOut',
    tableName:'CBS内部户交易记录导出'
});
const templateConfig = reactive({
    fileName:'businessFinance',
    apiPath:'CBS.downloadMB',
    param:{type:5},
    templateName:'CBS内部户交易记录模版'
});

const {
    form,
    page,
    tableOption,
    tableData,
    selectedArr,
    onQuery,
    onReset,
    onPageChange,
    getData,
    handleSelectionChange,
    onExport
}  = tableCommonFun(interfaceConfig,exportConfig);

form.value = {
    companyName:'',
    innerAccountId:'',
    tradeType:'',
    dateTime:'',
    startTime:'',
    endTime:'',
    otherAccountName:''
}

tableOption.value = [
    { label: "交易流水号 ", prop: "tradeNo", minWidth: 100 },
    { label: "交易日期 ", prop: "tradeTime", minWidth: 100 },
    { label: "起息日期 ", prop: "valueTime", minWidth: 100 },
    { label: "单位名称 ", prop: "companyName", minWidth: 100 },
    { label: "账号", prop: "account", minWidth: 100 },
    { label: "账户名称 ", prop: "accountName", minWidth: 100 },
    { label: "对方账户名称 ", prop: "otherAccountName", minWidth: 120 },
    { label: "对方账号 ", prop: "otherAccount", minWidth: 100 },
    { label: "对方开户行 ", prop: "otherBank", minWidth: 100 },
    { label: "交易类型 ", prop: "tradeType", minWidth: 100 },
    { label: "借贷类型 ", prop: "loanType", minWidth: 100 },
    { label: "用途", prop: "purpose", minWidth: 100 },
    { label: "交易金额", prop: "tradeAmount", minWidth: 100 },
    { label: "账户余额", prop: "accountBalance", minWidth: 100 },
    { label: "关联流水号", prop: "relateTradeNo", minWidth: 100 },
    { label: "内部户账号", prop: "innerAccountId", minWidth: 100 },
];

const companyList = ref([]);
/**
 * 查公司列表
 */
const getCompanyList = () => {
    companyApi.getList({}).then(res=>{
        companyList.value = res.data;
    });
}

const visible = ref(false);
const interestForm = reactive({
    startTime:"",
    endTime:"",
    valueTime:''
});

/**
 * 新增计息计算
 *
 */
const addHandle = () => {
    if(selectedArr.value.length===0){
        ElMessage.warning('请选择一条要操作的数据!');
        return;
    }
    visible.value = true;
}

/**
 * 计提计算
 *
 */
const interestCount = (param,callback) => {
    bankManagementApi.interestCount(param).then(res=>{
        if(res.code === '200'){
            if(callback)callback(res);
        }
    });
}

/**
 * 新增计息计算
 *
 */
const addInterest = (param,callback,err_callback) => {
    bankManagementApi.addInterest(param).then(res=>{
        if(res.code === '200'){
            if(callback)callback();
        }else{
            if(err_callback)err_callback();
        }
    });
}

/**
 * 新增计息计算确认
 *
 */
const onConfirm = () => {
    if(amountMoney.value===0){
        ElMessage.warning('计算计提计算结果不能为零,请点击计算按钮!');
        return;
    }
    formRef.value.validate((valid, fields) => {
        if (valid) {
            addInterest({
                loanRecordId:selectedArr.value[0].loanRecordId,
                startTime:interestForm.startTime,
                endTime:interestForm.endTime,
                calculateAmount:selectedArr.value[0].loanAmount,
                interestAmount:amountMoney.value,
            },(res)=>{
                setTimeout(()=>{
                    ElMessage.success('操作成功!');
                },1500);
                visible.value = false;
                publicTableRef.value.clearSelection();
                selectedArr.value = [];
                page.current = 1;
                page.size = 10;
                getData({
                    pageNum:1,
                    pageSize:10
                });
            },()=>{
                ElMessage.error('操作失败,请联系管理员!');
                visible.value = false;
                publicTableRef.value.clearSelection();
                selectedArr.value = [];
                page.current = 1;
                page.size = 10;
                getData({
                    pageNum:1,
                    pageSize:10
                });
            });
        } else {
            console.log('error submit!', fields)
        }
    });
}

/**
 * 计提时间被选择时触发
 *
 */
const onChange = (e) => {
    if(e===null || (e instanceof Array && e.length===0)){
        form.value.startTime = '';
        form.value.endTime = '';
        return;
    }
    form.value.startTime = e[0];
    form.value.endTime = e[1];
}

/**
 * 交易日期
 */
const datePickerChange = (e) => {
    form.startTime = '';
    form.endTime = '';
    if(e){
        form.value.startTime = e[0];
        form.value.endTime = e[1];
    }
}


const importFilePopupRef = ref(null);

/**
 * 导入
 */
const onImport = () => {
    importFilePopupRef.value.openPopup();
}



onMounted(()=>{
    getData({
        pageNum:1,
        pageSize:50
    });
});
</script>

<style lang="scss" scoped>
.internalTransactionRecordsPage{
  .search-wrap{
    .search-item{

      .el-input{
        width: auto !important;
      }
    }
    .search-buttons{
      .icon-export{
        width: 18px;
        height: 18px;
      }
    }
  }
}

.public-dialog-content{
  .dialog-title-box{
    height: 30px;
    span{
      font-size: 15px;
      font-weight: 600;
    }
  }
}
</style>
